package com.cet.test.ybb;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Object> container = Arrays.asList("a", "b", "c", "d");
        List<Object> container1 = Arrays.asList("1", "2", "3", "4");
        List<Object> container2 = Arrays.asList("A", "B", "C", "D");
        List<List> lists = cartesianProduct(container, container1, container2);
        System.out.println(lists);
        System.out.println("Breakpoint");
    }


    public static List<List> cartesianProduct(List... lists) {
        List<List> first = transformation(lists[0]);
        for (int i = 0; i < lists.length - 1; i++) {
            first = match(first, lists[i + 1]);
        }
        return first;
    }

    public static List<List> match(List<List> lists, List container1) {
        List<List> r = new ArrayList<>();
        for (List list : lists) {
            for (Object o : container1) {
                List list1 = new ArrayList();
                list1.addAll(list);
                list1.add(o);
                r.add(list1);
            }
        }
        return r;
    }

    /**
     * 将[a,b,c,d]转换成
     * [
     * [a],[b],[c],[d]
     * ]
     * why?
     * cos array便于做插入操作
     */
    private static List<List> transformation(List container) {
        List<List> r = new ArrayList<>();
        for (Object o : container) {
            List list = new ArrayList();
            list.add(o);
            r.add(list);
        }
        return r;
    }


}

